# -*- coding : utf-8 -*-
# coding: utf-8
"""
集中了项目中所需的工具方法
"""
import time
import math
from datetime import datetime, timezone
from flask import current_app

"""
将时间的秒数转换为适合人阅读的 'd 天, h 小时, m 分钟, s 秒' 的格式，用于日志打印等目的
"""


def friendlyDuration(durationTime):
    day = 24 * 60 * 60
    hour = 60 * 60
    min = 60
    if durationTime < 60:
        return "%d 秒" % math.ceil(durationTime)
    elif durationTime > day:
        days = divmod(durationTime, day)
        return "%d 天, %s" % (int(days[0]), friendlyDuration(days[1]))
    elif durationTime > hour:
        hours = divmod(durationTime, hour)
        return '%d 小时, %s' % (int(hours[0]), friendlyDuration(hours[1]))
    else:
        mins = divmod(durationTime, min)
        return "%d 分钟, %d 秒" % (int(mins[0]), math.ceil(mins[1]))


def nowTimeText4China():
    # TODO 可以移动到Config中统一管理时区设置
    # cst_tz = timezone('Asia/Shanghai')
    cst_tz = current_app.config['CST_TZ']
    nowTimeText = datetime.utcnow().replace(tzinfo=timezone.utc).astimezone(cst_tz).strftime('%Y-%m-%d %H:%M:%S')
    return nowTimeText
